package com.chen.script.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.chen.base.utils.PropertyUtil;
import com.chen.base.utils.file.IoFileUtil;
import com.chen.base.utils.file.WebFileUtil;
import com.chen.base.utils.file.ZipUtil;
import com.chen.root.base.FileChunk;
import com.chen.root.base.R;
import com.chen.root.oclass.vo.FileAttrVO;
import com.chen.root.oclass.vo.FileStructureVO;
import com.chen.script.config.CommonProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author ChenJinLong
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/file")
@Api(value = "file", tags = "文件服务API")
public class FileController {

    @Resource
    private CommonProperties commonProperties;

    @PostMapping("/download")
    @ApiOperation(value = "文件下载",notes = "文件下载")
    public void downloadFile(@NotBlank(message = "文件路径不能为空") String filePath) {
        log.info("文件下载 -> {}", filePath);
        WebFileUtil.downToClient(new File(filePath));
    }

    @PostMapping("/download/chunk")
    @ApiOperation(value = "文件分块下载",notes = "文件分块下载")
    public void chunkDownload(FileChunk fileChunk){
        log.info("文件数据块下载 -> {}", fileChunk);
        WebFileUtil.downToClient(fileChunk);
    }

    @PostMapping("/structure")
    @ApiOperation(value = "文件或文件夹结构",notes = "文件或文件夹结构")
    public R<FileStructureVO> fileStructure(String parentDir){
        return R.ok(IoFileUtil.fileStructure(parentDir, null));
    }

    @PostMapping("/part")
    @ApiOperation(value = "文件分片",notes = "文件分片")
    public R<List<FileAttrVO>> part(@NotBlank(message = "文件路径不能为空") String filePath){
        String partDirPath = commonProperties.getTempFileDir() + "\\" + IdUtil.objectId();
        IoFileUtil.partFile(filePath, partDirPath, () -> System.currentTimeMillis() + "");
        FileStructureVO vo = IoFileUtil.fileStructure(partDirPath, null);
        return R.ok(vo.getFileAttrs());
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除文件或文件夹",notes = "删除文件或文件夹")
    public R<String> del(@NotBlank(message = "文件路径不能为空") String filePath) {
        log.info("删除文件或文件夹 -> {}", filePath);
        FileUtil.del(filePath);
        return R.ok();
    }

    @PostMapping("/md5")
    @ApiOperation(value = "计算文件MD5",notes = "计算文件MD5")
    public R<String> md5(@NotBlank(message = "文件路径不能为空") String filePath) {
        long s = System.currentTimeMillis();
        String md5 = IoFileUtil.md5(filePath);
        log.info("计算文件MD5耗时: {}s", (System.currentTimeMillis() - s) / 1000);
        return R.ok(md5);
    }

    @Deprecated
    @GetMapping("/newestDmpFile")
    @ApiOperation(value = "打包ORACLE-DMP文件后下载",notes = "打包ORACLE-DMP文件后下载")
    public void newestDmpFile() throws Exception {
        String bakFilePath = PropertyUtil.getProperty("oracle.bak-file-path");
        if (StrUtil.isNotBlank(bakFilePath)){
            log.error("配置 oracle.bak-file-path 不存在!");
            return;
        }
        String targetPath = bakFilePath + "//" + DateUtil.date().toDateStr();
        String zipFilePath = bakFilePath + "//" + IdUtil.fastSimpleUUID() + ".zip";

        File dirFile = new File(targetPath);
        if (dirFile.exists() && dirFile.isDirectory()){
            ZipUtil.toZip(targetPath, Files.newOutputStream(Paths.get(zipFilePath)));
            WebFileUtil.downToClient(new File(zipFilePath));
            FileUtil.del(new File(zipFilePath));
        }
    }

}
